<template>
  <div class="about">
    <!-- 当 id 存在时 为编辑 否则是 新建 -->
    <h1>{{id ? "编辑" : "新建"}}英雄</h1>
    <el-form label-width="120px" @submit.native.prevent="save">
      <el-tabs type="border-card">
        <el-tab-pane label="基础信息">
          <!-- 名字 -->
          <el-form-item label="名称">
            <el-input v-model="model.name"></el-input>
          </el-form-item>
          <!-- 称号 -->
          <el-form-item label="称号">
            <el-input v-model="model.title"></el-input>
          </el-form-item>
          <!-- 头像 -->
          <el-form-item label="图标">
            <el-upload
              class="avatar-uploader"
              :action="$http.defaults.baseURL + '/upload' "
              :show-file-list="false"
              :on-success="afterUpload"
            >
              <img v-if="model.avater" :src="model.avater" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <!-- 背景图上传 -->
          <el-form-item label="banner">
            <el-upload
              class="avatar-uploader"
              :action="$http.defaults.baseURL + '/upload' "
              :show-file-list="false"
              :on-success="bannerUpload"
            >
              <img v-if="model.banner" :src="model.banner" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <!-- 职业选择 -->
          <el-form-item label="职业">
            <el-select v-model="model.categories" multiple>
              <el-option
                v-for="items of categories"
                :key="items._id"
                :label="items.name"
                :value="items._id"
              ></el-option>
            </el-select>
          </el-form-item>
          <!-- 难度打分 -->
          <el-form-item label="难度">
            <el-rate style="margin-top:0.6rem" :max="9" show-score v-model="model.scores.difficult"></el-rate>
          </el-form-item>
          <el-form-item label="攻击">
            <el-rate style="margin-top:0.6rem" :max="9" show-score v-model="model.scores.attack"></el-rate>
          </el-form-item>
          <el-form-item label="技能">
            <el-rate style="margin-top:0.6rem" :max="9" show-score v-model="model.scores.skills"></el-rate>
          </el-form-item>
          <el-form-item label="生存">
            <el-rate style="margin-top:0.6rem" :max="9" show-score v-model="model.scores.survive"></el-rate>
          </el-form-item>
          <!-- 出装推荐 -->
          <el-form-item label="顺风出装">
            <el-select v-model="model.items1" multiple>
              <el-option
                v-for="items of items"
                :key="items._id"
                :label="items.name"
                :value="items._id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="逆风出装">
            <el-select v-model="model.items2" multiple>
              <el-option
                v-for="items of items"
                :key="items._id"
                :label="items.name"
                :value="items._id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="铭文推荐">
            <el-select v-model="model.mingwen" multiple>
              <el-option
                v-for="items of items"
                :key="items._id"
                :label="items.name"
                :value="items._id"
              ></el-option>
            </el-select>
          </el-form-item>
          <!-- 技巧 -->
          <el-form-item label="使用技巧">
            <el-input type="textarea" v-model="model.usageTips"></el-input>
          </el-form-item>
          <el-form-item label="对抗技巧">
            <el-input type="textarea" v-model="model.battleTips"></el-input>
          </el-form-item>
          <el-form-item label="团战技巧">
            <el-input type="textarea" v-model="model.teamTips"></el-input>
          </el-form-item>
        </el-tab-pane>

        <el-tab-pane label="技能信息">
          <el-button @click="model.skills.push({})">
            <i class="el-icon-plus">添加技能</i>
          </el-button>
          <el-row type="flex" style="flex-wrap:wrap">
            <el-col :md="12" v-for="(item,i) in model.skills" :key="i">
              <el-form-item label="名称">
                <el-input v-model="item.name"></el-input>
              </el-form-item>

              <el-form-item label="图标">
                <el-upload
                  class="avatar-uploader"
                  :action="$http.defaults.baseURL + '/upload'"
                  :headers="getAuthHeaders()"
                  :show-file-list="false"
                  :on-success="res =>$set(item , 'icon', res.url)"
                >
                  <img v-if="item.icon" :src="item.icon" class="avatar" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>

              <el-form-item label="冷却">
                <el-input v-model="item.delay"></el-input>
              </el-form-item>

              <el-form-item label="消耗">
                <el-input v-model="item.cost"></el-input>
              </el-form-item>

              <el-form-item label="描述">
                <el-input type="textarea" v-model="item.description"></el-input>
              </el-form-item>

              <el-form-item label="小提示">
                <el-input type="textarea" v-model="item.tips"></el-input>
              </el-form-item>
              <el-button
                @click="model.skills.splice(i,1)"
                style="margin-left: 15rem;margin-bottom: 1rem;"
                size="small"
                type="danger"
              >删除</el-button>
            </el-col>
          </el-row>
        </el-tab-pane>

        <el-tab-pane label="最佳搭档">
          <el-button @click="model.parteners.push({})">
            <i class="el-icon-plus">添加英雄</i>
          </el-button>
          <el-row type="flex" style="flex-wrap:wrap">
            <el-col :md="12" v-for="(item,i) in model.parteners" :key="i">
              <el-form-item label="名称">
                <el-select filterable v-model="item.hero">
                  <el-option
                    v-for="hero in heroes"
                    :key="hero._id"
                    :value="hero._id"
                    :label="hero.name"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="描述">
                <el-input type="textarea" v-model="item.description"></el-input>
              </el-form-item>
              <el-button
                @click="model.parteners.splice(i,1)"
                style="margin-left: 15rem;margin-bottom: 1rem;"
                size="small"
                type="danger"
              >删除</el-button>
            </el-col>
          </el-row>
        </el-tab-pane>

        <el-tab-pane label="被谁克制">
          <el-button @click="model.restraint.push({})">
            <i class="el-icon-plus">添加英雄</i>
          </el-button>
          <el-row type="flex" style="flex-wrap:wrap">
            <el-col :md="12" v-for="(item,i) in model.restraint" :key="i">
              <el-form-item label="名称">
                <el-select filterable v-model="item.hero">
                  <el-option
                    v-for="hero in heroes"
                    :key="hero._id"
                    :value="hero._id"
                    :label="hero.name"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="描述">
                <el-input type="textarea" v-model="item.description"></el-input>
              </el-form-item>
              <el-button
                @click="model.restraint.splice(i,1)"
                style="margin-left: 15rem;margin-bottom: 1rem;"
                size="small"
                type="danger"
              >删除</el-button>
            </el-col>
          </el-row>
        </el-tab-pane>

        <el-tab-pane label="克制谁">
          <el-button @click="model.enemy.push({})">
            <i class="el-icon-plus">添加英雄</i>
          </el-button>
          <el-row type="flex" style="flex-wrap:wrap">
            <el-col :md="12" v-for="(item,i) in model.enemy" :key="i">
              <el-form-item label="名称">
                <el-select filterable v-model="item.hero">
                  <el-option
                    v-for="hero in heroes"
                    :key="hero._id"
                    :value="hero._id"
                    :label="hero.name"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="描述">
                <el-input type="textarea" v-model="item.description"></el-input>
              </el-form-item>
              <el-button
                @click="model.enemy.splice(i,1)"
                style="margin-left: 15rem;margin-bottom: 1rem;"
                size="small"
                type="danger"
              >删除</el-button>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
      <el-form-item style="margin-top:1rem">
        <el-button type="primary" native-type="submit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  // 获取路由里传过来的 需要编辑的 分类的 id
  props: {
    id: {}
  },
  data() {
    return {
      categories: [],
      items: [],
      model: {
        items1: [],
        items2: [],
        heroes: [],
        skills: [],
        // 克制谁
        enemy: [],
        // 最佳搭档
        parteners: [],
        // 被谁克制
        restraint: [],
        scores: {
          difficult: 0,
          skills: 0,
          attack: 0,
          survive: 0
        }
      }
    };
  },
  created() {
    //   当 id 存在时 触发 fetch 函数
    // this.fetchParents();
    this.fetchItems();
    this.fetchCategories();

    this.id && this.fetch();
    this.fetchHeroes();
  },
  methods: {
    getAuthHeaders() {
      return {
        Authorization: `Bearer${localStorage.token || ""}`
      };
    },
    afterUpload(res) {
      this.$set(this.model, "avater", res.url);
    },
    bannerUpload(res) {
      this.$set(this.model, "banner", res.url);
    },
    async save() {
      let res;
      if (this.id) {
        res = await this.$http.put(`/rest/heroes/${this.id}`, this.model);
        console.log(res);
        console.log(this.model.enemy)
      } else {
        res = await this.$http.post("/rest/heroes", this.model);
      }
      this.$router.push("/heroes/list");

      this.$message({
        type: "success",
        message: "保存成功"
      });
    },
    async fetch() {
      const res = await this.$http.get(`/rest/heroes/${this.id}`);
      this.model = Object.assign({}, this.model, res.data);
    },
    async fetchCategories() {
      const res = await this.$http.get("/rest/categories");
      this.categories = res.data;
    },
    async fetchItems() {
      const res = await this.$http.get("/rest/items");
      this.items = res.data;
    },
    async fetchHeroes() {
      const res = await this.$http.get("/rest/heroes");
      this.heroes = res.data;
    }
  }
};
</script>

<style scope>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 5rem;
  height: 5rem;
  line-height: 5rem;
  text-align: center;
}
.avatar {
  width: 5rem;
  height: 5rem !important;
  display: block;
}
</style>